package cn.iocoder.yudao.module.trade.controller.admin.delivery;

import cn.hutool.core.net.LocalPortGenerater;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.sf.*;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.sf.printResult.PrintResultInfo;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.sf.printResult.PrintResultResp;
import cn.iocoder.yudao.module.trade.service.delivery.SfExpressService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sf.csim.express.service.CallExpressServiceTools;
import com.sf.csim.express.service.HttpClientUtil;
import com.sf.csim.express.service.code.ExpressServiceCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Slf4j
@Tag(name = "管理后台 - 顺丰")
@RestController
@RequestMapping("/trade/delivery/express/sf")
@Validated
public class SfController {
    /**
     * 丰桥新沙箱测试顾客编码  Yg4Zf06w_sxZs3A5D
     * 校验码  3Xdk1jqeG1Xod9nUXus8Op7DNOkchTnw
     **/
    private static final String CLIENT_CODE = "YQXXKGEFBMK1";  //此处替换为您在丰桥平台获取的顾客编码
    private static final String CHECK_WORD = "DhG7LEZmEQWin9tfinW0b2W4KDHGd651";//此处替换为您在丰桥平台获取的校验码


    //沙箱环境的地址 -PRO
    private static final String CALL_URL_BOX = "https://sfapi-sbox.sf-express.com/std/service";

    @Resource
    private SfExpressService sfExpressService;

    @PostMapping("/submit")
    @Operation(summary = "提交顺丰订单")
//    @PreAuthorize("@ss.hasPermission('trade:delivery:pick-up-store:create')")
    public CommonResult<JSONObject> expressCreateOrder(@Valid @RequestBody SfCreateReqVO reqVO) throws UnsupportedEncodingException {

        JSONObject result = sfExpressService.expressCreateOrder(reqVO);

        return success(result);
    }


    @PostMapping("/print")
    @Operation(summary = "打印顺丰订单")
//    @PreAuthorize("@ss.hasPermission('trade:delivery:pick-up-store:create')")
    public CommonResult<JSONObject> printWaybills(@Valid @RequestBody SfPrintReqVO reqVO) throws UnsupportedEncodingException {

        JSONObject obj = sfExpressService.printWaybills(reqVO);
        return success(obj);
    }

    @PostMapping("/channel")
    @Operation(summary = "取消顺丰订单")
//    @PreAuthorize("@ss.hasPermission('trade:delivery:pick-up-store:create')")
    public CommonResult<JSONObject> channelExpressOrder(@Valid @RequestBody SfChannelReqVO reqVO) throws UnsupportedEncodingException {

        JSONObject result = sfExpressService.channelExpressOrder(reqVO);

        return success(result);
    }
}
